package view;

/*WhiteboardFrame.java
 *CS193J Summer 20001
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;

import socket.ui.Commands;
import socket.ui.UserInterface;
import constants.WhiteboardConstants;
import figures.ShapeContainer;

/**
 * Represents the structure of the whiteboard; with MenuBar, Palette and
 * ShapeContainer as Components
 * 
 * @see MenuBar
 * @see Palette
 * @see Container
 * @see ShapeContainer
 * 
 * @author Jatinder Pal Singh
 */

public class WhiteboardFrame extends Frame implements UserInterface {

	Commands client;

	private MenuBar dMenuBar;
	private Palette dPalette;
	private Container _dShapeContainer;
	private MenuItem[] dMenuItems = new MenuItem[WhiteboardConstants.NO_OF_MENU_ITEMS];
	private MenuItem aMenuItem;

	private void createMenuBar() {
		int i = 0;
		dMenuBar = new MenuBar();
		setMenuBar(dMenuBar);

		/** Adding file menu items */
		Menu fileMenu = new Menu("File");
		aMenuItem = new MenuItem(WhiteboardConstants.NEW_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		fileMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.IMPORT_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		fileMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.SAVE_AS_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		fileMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.PRINT_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		fileMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.EXIT_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		fileMenu.add(aMenuItem);
		dMenuBar.add(fileMenu);

		/** Adding edit menu items */
		Menu editMenu = new Menu("Edit");
		aMenuItem = new MenuItem(WhiteboardConstants.CUT_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.COPY_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.PASTE_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.CLEAR_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.FRONT_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		aMenuItem = new MenuItem(WhiteboardConstants.BACK_MENU_ITEM);
		aMenuItem.setEnabled(false);
		dMenuItems[i] = aMenuItem;
		i++;
		editMenu.add(aMenuItem);
		dMenuBar.add(editMenu);

		/** Adding help menu items */
		Menu helpMenu = new Menu("Help");
		aMenuItem = new MenuItem(WhiteboardConstants.ABOUT_MENU_ITEM);
		dMenuItems[i] = aMenuItem;
		i++;
		helpMenu.add(aMenuItem);
		dMenuBar.add(helpMenu);

		WhiteboardWindowAdapter adapter = new WhiteboardWindowAdapter(this);
		addWindowListener(adapter);
	}

	/** Returns Array of all MenuItems */
	public MenuItem[] getAllMenuItems() {
		return dMenuItems;
	}

	private void createPalette() {
		dPalette = new Palette();
	}

	public Palette getPalette() {
		return dPalette;
	}

	private void createShapeContainer() {
		_dShapeContainer = new ShapeContainer();
	}

	public Container getShapeContainer() {
		return _dShapeContainer;
	}

	/**
	 * Construcs a resizeable WhiteBoard with BorderLayout
	 * 
	 * @args title of the Whiteboard
	 */
	public WhiteboardFrame(String title) {
		super(title);

		createMenuBar();
		createPalette();
		createShapeContainer();

		setLayout(new BorderLayout());
		add(dPalette, BorderLayout.SOUTH);
		add(_dShapeContainer, BorderLayout.CENTER);

		setResizable(true);
		setBackground(Color.lightGray);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				if (client != null)
					client.exitAction();
				// System.out.println("windowClosing()"); // TODO Auto-generated
				// Event stub windowClosing()
			}
		});
	}

	public void failureButton1ctionPerformed() {
		if (client != null)
			;// client.injectFailure(Integer.parseInt("2"));
	}

	public void failureRatioActionPerformed() {
		if (client != null)
			;// client.setFailureRatio(Integer.parseInt("10"));
	}

	public void closeInputActionPerformed() {
		if (client != null)
			;// client.closeInput();
	}

	public void openInputActionPerformed() {
		if (client != null)
			;// client.openInput();
	}

	public void logFailures(int failure) {
		// getFailsTextField().setText("" + failure);

	}

	public void setClient(Commands client) {
		this.client = client;
	}

}
